package com.zzuli.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.zzuli.order.entity.Emp;
import com.zzuli.order.mapper.EmpDao;
import com.zzuli.order.service.EmpService;
import lombok.Synchronized;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.*;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Override
    public void getData() throws IOException {
        Integer count = empDao.selectCount(Wrappers.emptyWrapper());
        int size = count % 100000 == 0 ? count / 100000 : count / 100000 + 1;
        boolean[] visited = new boolean[size];

        List<Future> list = new ArrayList<>();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(6,100,60L,TimeUnit.SECONDS,new ArrayBlockingQueue<>(50));
        //ExecutorService executor = Executors.newCachedThreadPool();
        for (int j = 0; j < visited.length; j++) {
            Future<?> future = executor.submit(new Runnable() {
                @Override
                public void run() {
                    int start = -1;
                    synchronized (visited) {
                        for (int i = 0; i < visited.length; i++) {
                            if (visited[i] == false) {
                                start = i;
                                visited[i] = true;
                                break;
                            }
                        }
                    }
                    QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
                    queryWrapper.ge("id", start * 100000).last("limit 100000");
                    List<Emp> empsi = empDao.selectList(queryWrapper);
                    try {
                        export(empsi, start * 100000, start * 100000 + 100000);
                        empsi.clear();
                        empsi = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

            list.add(future);
        }

        executor.shutdown();
        while (true){
            if (executor.isTerminated()){
                break;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).isDone());
        }
    }

    @Override
    public void getAllData() throws IOException {
        int l = 0,size = 1000000;
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id",0).last("limit "+size);
        List<Emp> emps = empDao.selectList(queryWrapper);
        export(emps, 0, size);

        queryWrapper.gt("id",1 * size).last("limit "+size);
        emps = empDao.selectList(queryWrapper);
        export(emps, 1, size);

        queryWrapper.gt("id", 2 * size).last("limit "+size);
        emps = empDao.selectList(queryWrapper);
        export(emps, 2, size);
    }

    public void export(List<Emp> datas,int l ,int r) throws IOException {
        if (l < 0) return;
        //创建工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();//这里也可以设置sheet的Name

        //创建工作表的行
        int size = 50000;
        int k = datas.size() % 50000 == 0 ? datas.size() / 50000 : datas.size() / 50000 + 1;
        int cur = 0;
        for (int j = 0; j < k; j++) {
            //创建工作表对象
            HSSFSheet sheet = workbook.createSheet(String.valueOf(j));
            HSSFRow row2 = sheet.createRow(0);//设置第一行，从零开始
            row2.createCell(0).setCellValue("ID");//第一行第三列为aaaaaaaaaaaa
            row2.createCell(1).setCellValue("no");//第一行第一列为日期
            row2.createCell(2).setCellValue("name");
            row2.createCell(3).setCellValue("job");
            row2.createCell(4).setCellValue("mgr");
            row2.createCell(5).setCellValue("hiredate");
            row2.createCell(6).setCellValue("sal");
            row2.createCell(7).setCellValue("comm");
            row2.createCell(8).setCellValue("deptno");
            if (j == k -1 && datas.size() % 50000 > 0) size =  datas.size() % 50000;
            for (int i = 0; i < size; i++, cur++) {
                HSSFRow row = sheet.createRow(i + 1);//设置第一行，从零开始
                row.createCell(0).setCellValue(datas.get(cur).getId());//第一行第三列为aaaaaaaaaaaa
                row.createCell(1).setCellValue(datas.get(cur).getEmpno());//第一行第一列为日期
                row.createCell(2).setCellValue(datas.get(cur).getEname());
                row.createCell(3).setCellValue(datas.get(cur).getJob());
                row.createCell(4).setCellValue(datas.get(cur).getMgr());
                row.createCell(5).setCellValue(datas.get(cur).getEname());
                row.createCell(6).setCellValue(datas.get(cur).getSal());
                row.createCell(7).setCellValue(datas.get(cur).getComm());
                row.createCell(8).setCellValue(datas.get(cur).getDeptno());
                //   row.createCell(6).setCellValue(datas.get(i).getY());
            }
            workbook.setSheetName(0,"sheet"+j);//设置sheet的Name
        }


        //文档输出
        FileOutputStream out = new FileOutputStream("E:\\多线程导出订单测试\\" + l +"-" +r +".xls");
        workbook.write(out);
        out.close();
    }
}
